package com.czl.module_user.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.util.DialogHelper
import com.czl.module_user.BR
import com.czl.module_user.R
import com.czl.module_user.adapter.AddRecordsAdapter
import com.czl.module_user.databinding.FragmentAddRecordsBinding
import com.czl.module_user.viewmodel.AddPeopleViewModel
import com.czl.module_user.viewmodel.AddRecordsViewModel

/**
 *
 * @Description:添加记录
 * @Author: XCH
 * @CreateDate: 2022/1/7 8:43
 */
@Route(path = AppConstants.Router.User.F_ADD_RECORDS)
class AddRecordsFragment : BaseFragment<FragmentAddRecordsBinding, AddRecordsViewModel>() {

    private lateinit var mAdapter: AddRecordsAdapter

    override fun initContentView(): Int {
        return R.layout.fragment_add_records
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("添加记录")
        initAdapter()
        reload()
    }

    private fun initAdapter() {
        mAdapter = AddRecordsAdapter(this)
        binding.ryCommon.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            handleRecyclerviewData(
                data == null,
                data as MutableList<*>?,
                mAdapter,
                binding.ryCommon,
                binding.smartCommon,
                1,
                true
            )
        })

        viewModel.uc.renewEvent.observe(this, {
            //申请状态(1:待审核,2:审核通过,3:审核不通过)
//            if (it.applyStatus == 3) {
                startContainerActivity(AppConstants.Router.User.F_ADD_PEOPLE, Bundle().apply {
                    putString(AppConstants.BundleKey.PEOPLE_ID, it.id)
                    putInt(AppConstants.BundleKey.PEOPLE_FROM, AddPeopleViewModel.FROM_EDIT)
                })
//            } else {
//                startContainerActivity(AppConstants.Router.User.F_PEOPLE_DETAIL, Bundle().apply {
//                    putString(AppConstants.BundleKey.PEOPLE_ID, it.id)
//                    putInt(AppConstants.BundleKey.PEOPLE_FROM, 1)
//                })
//            }
        })

        viewModel.uc.deleteEvent.observe(this, {
            DialogHelper.showBaseDialog(requireContext(), "", "确定删除该人员吗？") {
                viewModel.deleteApply(it)
            }
        })

        viewModel.uc.deleteSuccessEvent.observe(this, {
            reload()
        })


    }

    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }
}